package sv.com.seguros.dso.asg;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import sv.com.seguros.dso.BaseServiceImpl;
import sv.com.seguros.dto.asg.AsgReclamo;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.util.WebUtilidades;

public class AsgReclamoServiceImpl extends BaseServiceImpl implements AsgReclamoService {
	
	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = {Exception.class, WebException.class})
	public void save(AsgReclamo asgReclamo) throws WebException{
		try{
			asgReclamo.setAsgReclamoNumero("0");
			super.save(asgReclamo);
			asgReclamo.setAsgReclamoNumero(StringUtils.leftPad(asgReclamo.getAsgReclamoId().toString(), 8, "0"));
			super.update(asgReclamo);
		}catch(Exception e){
			logger.error("Exception:", e);
			throw new WebException("Problemas al tratar de guardar la informaci\u00F3n");
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = {Exception.class, WebException.class})
	public void delete(Long asgReclamoId) throws WebException{
		try{
			String hqlQuery = "delete from AsgSeguimiento where asgReclamo.asgReclamoId = :asgReclamoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("asgReclamoId", asgReclamoId);
			executeNonHibernateQuery(hqlQuery, parameters);
			hqlQuery = "delete from AsgReclamo where asgReclamoId = :asgReclamoId";
			executeNonHibernateQuery(hqlQuery, parameters);
		}catch(Exception e){
			logger.error("Exception:", e);
			throw new WebException("Problemas al tratar de eliminar la informaci\u00F3n del reclamo");
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = {Exception.class, WebException.class})
	public void finalizar(Long asgReclamoId) throws WebException{
		try{
			String hqlQuery = "update AsgReclamo set asgReclamoFinalizado = '1', asgReclamoFechaFinalizacion = :fecha where asgReclamoId = :asgReclamoId";
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("fecha", WebUtilidades.dateFormatAsYYYYMMDD.format(new Date()));
			parameters.put("asgReclamoId", asgReclamoId);
			executeNonHibernateQuery(hqlQuery, parameters);
		}catch(Exception e){
			logger.error("Exception:", e);
			throw new WebException("Problemas al tratar de actualizar informaci\u00F3n del reclamo");
		}
	}
	
	@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = {Exception.class, WebException.class})
	public void deleteAsgReclamo(Long asgReclamoId) throws Exception{
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("asgReclamoId", asgReclamoId);
		String hqlQuery = "delete from AsgSeguimiento where asgReclamo.asgReclamoId = :asgReclamoId)";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgContacto where asgReclamo.asgReclamoId = :asgReclamoId";
		executeNonHibernateQuery(hqlQuery, parameters);
		hqlQuery = "delete from AsgReclamo where asgReclamoId = :asgReclamoId";
		executeNonHibernateQuery(hqlQuery, parameters);
	}

}
